package com.bsth.repository.schedule;

import com.bsth.entity.schedule.CarConfigInfo;
import com.bsth.entity.schedule.TTInfoDetail;
import com.bsth.repository.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xu on 16/7/2.
 */
@Repository
public interface TTInfoDetailRepository extends BaseRepository<TTInfoDetail, Long> {

    @EntityGraph(value = "tTInfoDetail_xl_lp_qdz_zdz_tcc", type = EntityGraph.EntityGraphType.FETCH)
    @Override
    Page<TTInfoDetail> findAll(Specification<TTInfoDetail> spec, Pageable pageable);

    @EntityGraph(value = "tTInfoDetail_xl_lp_qdz_zdz_tcc", type = EntityGraph.EntityGraphType.FETCH)
    @Override
    List<TTInfoDetail> findAll(Specification<TTInfoDetail> spec);

    @EntityGraph(value = "tTInfoDetail_xl_lp_qdz_zdz_tcc", type = EntityGraph.EntityGraphType.FETCH)
    @Query("select cc from TTInfoDetail cc where cc.id=?1")
    TTInfoDetail findOneExtend(Long aLong);

//    TODO：在findOne中  @EntityGraph 无效
//    @EntityGraph(value = "tTInfoDetail_xl_lp_qdz_zdz_tcc", type = EntityGraph.EntityGraphType.FETCH)
//    @Override
//    TTInfoDetail findOne(Long aLong);

    @Query(value = "select max(tt.fcno) as mx from bsth_c_s_ttinfo_detail tt where tt.xl =?1 and tt.ttinfo =?2", nativeQuery = true)
    Long findMaxFcno(Integer xlid, Long ttinfoid);

    @Query(value = "select tt from TTInfoDetail tt where tt.xl.id = ?1 and tt.ttinfo.id = ?2 and tt.lp.id = ?3 order by tt.fcno asc")
    List<TTInfoDetail> findBcdetails(Integer xlId, Long ttinfoId, Long lpId);

    List<TTInfoDetail> findByTtinfoId(Long id);

    @Modifying
    @Query(value = "delete from TTInfoDetail t where t.ttinfo.id = ?1")
    void deleteByTtinfoIdWithModify(Long ttinfoid);

    @Modifying
    @Query(value = "delete from TTInfoDetail t where t.xl.id=?1 and t.ttinfo.id = ?2")
    void deltidc(int xl, Long ttinfoid);
}
